<template>
  <div class="update-name">
    <van-nav-bar title="设置昵称">
        <span slot="left" style="color: #1989fa" @click="$emit('cancel')">取消</span>
        <span slot="right" style="color: #1989fa"  @click="confirm">完成</span>
    </van-nav-bar>
    <div class="field-wrap">
        <van-field
            v-model.trim="localName"
            rows="2"
            autosize
            type="textarea"
            maxlength="7"
            placeholder="请输入留言"
            show-word-limit
        />
    </div>
  </div>
</template>

<script>
import { updateProfileInfo } from '@/api/user'
export default {
  name: 'UpdateName',
  components: {},
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      localName: this.value
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async confirm () {
      if (!this.localName.length) {
        this.$toast('昵称不能为空')
        return
      }
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true, // 静止背景点击
        duration: 0
      })
      try {
        await updateProfileInfo({ name: this.localName })
        this.$emit('input', this.localName)
        this.$toast.success('修改昵称成功')
      } catch (err) {
        this.$toast.fail('修改昵称失败')
      }
      // 关闭弹层
      this.$emit('cancel')
    }
  }
}
</script>

<style lang="less" scoped>
    .update-name{
        height: 100%;
        background-color: #f5f7f9;
        .field-wrap{
            padding: 20px;
        }
    }
</style>>
